package ru.autofan.logic;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class DaoSupporterImpl extends HibernateDaoSupport implements DaoSupporter {

	private Query getParametrisedQuery(String strQuery, Map<String, Object> params) {
		Query query = getSession().createQuery(strQuery);
		for (String key : params.keySet()) {
			query.setParameter(key, params.get(key));
		}
		return query;
    }
    
    private Query getUnParametrisedQuery(String strQuery) {
		Query query = getSession().createQuery(strQuery);
		return query;
    }
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getList(Class<T> clazz, String strQuery, Map<String, Object> params) {
		return getParametrisedQuery(strQuery, params).list();
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> getList(Class<T> clazz, String strQuery) {
		return getUnParametrisedQuery(strQuery).list();
	}


	@SuppressWarnings("unchecked")
	public <T> T getUniqueObject(Class<T> clazz, String strQuery) {
		return (T) getUnParametrisedQuery(strQuery).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public <T> T getUniqueObject(Class<T> clazz, String strQuery, Map<String, Object> params) {
		return  (T)getParametrisedQuery(strQuery, params).uniqueResult();
	}

}
